package oop.core.parsers;

import java.util.Scanner;
import oop.core.entity.Avatar;
import oop.core.entity.DerivedStats;
import oop.core.entity.Occupation;
import oop.core.item.Equipment;
import oop.core.item.Inventory;
import oop.core.map.Location;
/**
 * this class would have been a utility for saving and loading the game.
 * It takes in an avatar and formats all the information of the avatar and it's attribute classes
 * into a single string.  It also creates an avatar by reading in a formated string.
 * only part not implemented is the Inventory Parser.
 * @author srhinson
 *
 */
public class AvatarParser {
	static String del = "A~";
	
	//build an avatar from a text String
	//format of string is a data field for every field in constructor, seperated by
	public static String parseOut(Avatar avatar){
		String data = "";
		data=data.concat(avatar.getMoney()+"A~");//money
		if(avatar.getOcc()!=null){
		data=data.concat(OccupationParser.parseOut(avatar.getOcc())+"A~");
		}//occupation
		data=data.concat(avatar.getImageID()+"A~");//image
		data=data.concat(avatar.getName()+"A~");//name
		data=data.concat(avatar.getDirection()+"A~");//direction
		if(avatar.getLoc()!=null){
		data=data.concat(LocationParser.parseOut(avatar.getLoc())+"A~");//location
		}
		data=data.concat(avatar.isVisible()+"A~");//isvisible
		data=data.concat(EquipmentParser.parseOut(avatar.getEquipment()));
		if(avatar.getDerStats()!=null){
		data=data.concat(DerivedStatsParser.parseOut(avatar.getDerStats()));//derivedStats
		}
		return data;
	}
	
	//build a text file representing an avatar
	public static Avatar parseIn(String data){
		Scanner scanner = new Scanner(data);
		scanner.useDelimiter(del);
		int money = Integer.parseInt(scanner.next());
		Occupation occ=OccupationParser.parseIn(scanner.next());
		String image = scanner.next();
		String name=scanner.next();
		int direction=Integer.parseInt(scanner.next());
		Location loc=LocationParser.parseIn(scanner.next());
		boolean visable=Boolean.parseBoolean(scanner.next());
		Equipment equipment = EquipmentParser.parseIn(scanner.next());
		DerivedStats dirstats=DerivedStatsParser.parseIn(scanner.next());
		return new Avatar(money, occ, image, name, direction, loc, visable, equipment, new Inventory(), null, dirstats);
	}

}
